import { useRouter } from 'vue-router';

export function useNavigation() {
  const router = useRouter();

  // 普通跳转，支持路径参数 (params) 和查询参数 (query)
  const goTo = (
    path: string,
    options: { params?: Record<string, any>; query?: Record<string, any> } = {}
  ) => {
    if (options.params) {
      router.push({ name: path, params: options.params, query: options.query });
    } else {
      router.push({ path, query: options.query });
    }
  };

  // 替换跳转，支持路径参数 (params) 和查询参数 (query)
  const replaceTo = (
    path: string,
    options: { params?: Record<string, any>; query?: Record<string, any> } = {}
  ) => {
    if (options.params) {
      router.replace({
        name: path,
        params: options.params,
        query: options.query,
      });
    } else {
      router.replace({ path, query: options.query });
    }
  };

  // 返回上一级页面
  const goBack = () => router.back();

  return { goTo, replaceTo, goBack };
}
